﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Data;
using System.Windows.Media;
using Color = System.Windows.Media.Color;

namespace DataConfigWpf.converters
{
    public class StringTestConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            double val = 0.0;
            if (double.TryParse(val.ToString(), out val) && targetType == typeof(string))
            {
                return Math.Round(val, 2).ToString();
            }
            return val.ToString();
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            double val = 0.0;
            if (double.TryParse(val.ToString(), out val) && targetType == typeof(string))
            {
                return Math.Round(val, 2);
            }
            return val;
        }
    }


    public class ZZConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            SolidColorBrush ret = new SolidColorBrush(Color.FromRgb(0, 0, 0));
            if (value is Color && targetType == typeof(SolidColorBrush)) ret = new SolidColorBrush((Color)value);
            return ret;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            var ret = Color.FromRgb(255, 255, 255);
            if (value is SolidColorBrush && targetType == typeof(Color)) ret = ((SolidColorBrush)value).Color;
            return ret;
        }
    }


    public class VConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            Visibility ret = Visibility.Collapsed;
            if (targetType == typeof(Visibility) && int.TryParse(value.ToString(), out var val))
                ret = val > 0 ? Visibility.Visible : Visibility.Hidden;
            return ret;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            int ret = 0;
            if (targetType == typeof(int))
                ret = ((Visibility)value) == Visibility.Visible ? 1 : 0;
            return ret;
        }
    }


}
